package com.jcfk.eam.service.base;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.base.EamSopItemImgDao;
import com.jcfk.eam.domain.dto.base.EamSopItemImgDTO;
import com.jcfk.eam.domain.po.base.EamSopItemImg;
import com.jcfk.eam.domain.query.base.EamSopItemImgQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 标准作业项图片
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-08
 */
@Service
public class EamSopItemImgService extends BaseService<EamSopItemImgDao, EamSopItemImg> {

    /**
     * EamSopItemImg分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamSopItemImgDTO> queryByPage(DataPage<EamSopItemImgDTO> page, EamSopItemImgQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamSopItemImg是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamSopItemImgDTO dto) {
        QueryWrapper<EamSopItemImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入标准作业项图片
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamSopItemImgDTO dto) {

        // 保存
        EamSopItemImg item = new EamSopItemImg();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新标准作业项图片
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamSopItemImgDTO dto) {

        // 保存
        EamSopItemImg item = new EamSopItemImg();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除标准作业项图片(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamSopItemImgDTO dto) {

        EamSopItemImg item = new EamSopItemImg();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamSopItemImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamSopItemImg item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取标准作业项图片信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamSopItemImgDTO get(String id) {
        // 查询数据
        EamSopItemImg item = this.baseMapper.selectById(id);
        EamSopItemImgDTO dto = new EamSopItemImgDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamSopItemImgDTO>
     */
    public List<EamSopItemImgDTO> getList(EamSopItemImgQuery query) {
        // 查询条件
        QueryWrapper<EamSopItemImg> qw = new QueryWrapper<>();
        qw.lambda().eq(EamSopItemImg::getDeleted, 0);
        qw.lambda().eq(StrUtil.isNotEmpty(query.getSopItemId()), EamSopItemImg::getSopItemId, query.getSopItemId());
        // 查询数据
        List<EamSopItemImg> list = super.list(qw);

        return convertList(list, EamSopItemImgDTO.class);
    }

    /**
     * 根据主键获取标准作业项图片信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamSopItemImg item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

